﻿using AutoMapper;
using RestApp.UI.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace RestApp.UI.Models
{
	public class WorkGraphic : IMvcView<RestApp.BusinessLogic.WorkGraphic>
	{
		public int Id { get; set; }

		[Required]
		public Nullable<int> Year { get; set; }

		[Required]
		public Nullable<int> Month { get; set; }

		[Required]
		public Nullable<int> Day { get; set; }

		[Required]
		public Nullable<System.TimeSpan> StartTime { get; set; }

		[Required]
		public Nullable<System.TimeSpan> EndTime { get; set; }

		[Required]
		public Nullable<int> IdUser { get; set; }

		public string GraphicsDate 
		{ 
			get
			{
				return string.Format("{0}.{1}.{2}", Day, Month, Year);
			}
		}

		public string GraphicsTime
		{
			get
			{
				return string.Format("{0} - {1}", 
					string.Format("{0:hh\\:mm}", StartTime), 
					string.Format("{0:hh\\:mm}", EndTime));
			}
		}

		public string FactsTime
		{
			get
			{
				if(!FactsWasFilled)
				{
					return string.Empty;
				}
				var fact = FactOfWorks.First();
                if (fact.IsReasondForWorkCancelation)
                {
                    return string.Format("Уважительная причина отсутствия: " + fact.Comment);
                }
                else if (fact.NoWork)
                {
                    return string.Format("Прогул сотрудника. " + fact.Comment);
                }
                else
                { 
				    return string.Format("{0} - {1}",
					    string.Format("{0:hh\\:mm}", fact.TimeStart),
					    string.Format("{0:hh\\:mm}", fact.TimeEnd));
                }
			}
		}

		public bool FactsWasFilled
		{
			get
			{
				return FactOfWorks != null && FactOfWorks.Any();
			}
		}

		public virtual ICollection<RestApp.UI.Models.JobFact> FactOfWorks { get; set; }

		public BusinessLogic.WorkGraphic ToBusinessEntity()
		{
			return Mapper.Map<BusinessLogic.WorkGraphic>(this);
		}
	}
}